package com.dzf.dao;

import com.dzf.entity.*;
import com.mongodb.client.result.DeleteResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class BlogDao {
    @Autowired
    MongoTemplate mongoTemplate;

    /**
     *
     * @param pageNum 在业务层算从第几行开始
     * @param num 每页几行
     * @param likes 模糊查询内容
     * @return
     */
    public Map<String, Object> findFuzzy(int pageNum, int num, String likes){
        int total=0;
        //摘要
        Query query = new Query();
        if (likes!=null)
        {
            query.addCriteria(new Criteria().orOperator(Criteria.where("author").regex("^.*"+likes+".*$"),Criteria.where("title").regex("^.*"+likes+".*$"),Criteria.where("summary").regex("^.*"+likes+".*$")));
            total = (int) mongoTemplate.count(query, Blog.class);
        }
        query.skip(pageNum).limit(num);

        List<Blog> list = mongoTemplate.find(query, Blog.class);
        Map<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("list",list);
        return map;
    }

    public List<Blog> findByTops(String msg)
    {
        Query query = new Query();
        query.with(Sort.by(Sort.Direction.DESC,msg));
        query.limit(10);
        return mongoTemplate.find(query,Blog.class,"t_blog");
    }


    public List<Blog> findByPage(int pageNum,int num)
    {
        Query query = new Query();
        query.skip(pageNum).limit(num);

        return mongoTemplate.find(query,Blog.class);
    }

    /**
     *
     * @param blogid 博客的ID
     * @return 博客其他信息和博客内容的组合类
     */
    public BlogCont findById(String blogid)
    {
        Blog blog = mongoTemplate.findById(blogid, Blog.class);
        blog.setReadvolume(blog.getReadvolume()+1);
        mongoTemplate.save(blog);
        BlogContext context = mongoTemplate.findById(blog.getContextId(), BlogContext.class);
        BlogCont cont = new BlogCont(blog, context);
        return cont;
    }

    public boolean amend(BlogCont blogCont)
    {
        System.out.println(blogCont);
        //如果博客ID不为空，那么这次请求为更新
        if (blogCont.getBlog().getId()!=null)
        {
            //更新博客对应的内容
            mongoTemplate.save(blogCont.getBlogContext());
        }else {
            //先添加 博客内容 得到博客内容的ID
            BlogContext blogContext = mongoTemplate.save(blogCont.getBlogContext());
            //将获得的博客内容ID设置到博客对象中
            blogCont.getBlog().setContextId(blogContext.getId());
        }
        //更新博客对象
        Blog blog = mongoTemplate.save(blogCont.getBlog());
        System.out.println(blog);
        return blog!=null?true:false;
    }

    public Map<Object, Object> findByBlogType(String typeid, int pageNum)
    {
        Map<Object, Object> map = new HashMap<>();
        Query query = new Query();
        query.addCriteria(Criteria.where("typeid").is(typeid));
        int total = (int) mongoTemplate.count(query, Blog.class);
        map.put("total",total);
        query.skip(pageNum).limit(10);
        List<Blog> list = mongoTemplate.find(query, Blog.class);
        map.put("list",list);
        return map;
    }

    public List<Blog> findAll(String uid){

        Query query = new Query();
        query.addCriteria(Criteria.where("uid").is(uid));
        List<Blog> list = mongoTemplate.find(query, Blog.class);

        return list;
    }

    public Blog findBlogById(String blogid)
    {
        Query query = new Query();

        Blog blog = mongoTemplate.findById(blogid, Blog.class);
        return blog;
    }

    public Boolean delBlogCont(Blog blog)
    {
        DeleteResult num = mongoTemplate.remove(new Query(Criteria.where("id").is(blog.getContextId())), BlogContext.class);
        if (num.getDeletedCount()>0)
        {
            DeleteResult result1 = mongoTemplate.remove(new Query(Criteria.where("blogId").is(blog.getId())), Discuss.class);
            System.out.println("删除了 "+result1.getDeletedCount() + " 条评论");
            DeleteResult result = mongoTemplate.remove(new Query(Criteria.where("blogid").is(blog.getId())), Rate.class);
            System.out.println("删除了点赞信息" + result.getDeletedCount());
            num = mongoTemplate.remove(new Query(Criteria.where("id").is(blog.getId())), Blog.class);
        }
        return num.getDeletedCount()>0?true:false;
    }
}
